package ihm;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JLabel;

import outil.PropertyLoader;

public class ConfigFrame extends JFrame {

	private static final String msg_btn_up = "Veuillez appuyer sur la touche \"HAUT\" que utiliserez pour jouer.";
	private static final String msg_btn_right = "Veuillez appuyer sur la touche \"DROITE\" que utiliserez pour jouer.";
	private static final String msg_btn_down = "Veuillez appuyer sur la touche \"BAS\" que utiliserez pour jouer.";
	private static final String msg_btn_left = "Veuillez appuyer sur la touche \"GAUCHE\" que utiliserez pour jouer.";
	private static final String msg_btn_action = "Veuillez appuyer sur la touche \"ACTION\" que utiliserez pour jouer.";
	private static final String msg_btn_start = "Veuillez appuyer sur la touche \"START\" que utiliserez pour jouer.";

	JLabel Promptlabel;
	HiddenButton CloseButton;
	
	private static String FILE_PROPERTIES = "res/properties/cgi.properties";

	private String[] messages;
	
	private int occurence = 0;
	
	private Jeu jeu;

	public ConfigFrame(Jeu jeu) 
	{
		super("Configuration GAME N WATCH");
		this.jeu = jeu;
		initFrame();
		addKeyListener(new ConfigKeyListener());
		
		setVisible(true);
	}

	private void initFrame() {
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		this.setLocation(dim.width / 2 - 400 / 2, dim.height / 2 - 300 / 2);
		this.setLayout(null);
		setResizable(false);
		setSize(400, 300);
		setUndecorated(true);
		try 
		{
			BufferedImage bf = ImageIO.read(new File(
					"res/img/GNWConfigBackground.jpg"));
			this.setContentPane(new FrameBackGround(bf));
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		
		messages = new String[] { msg_btn_up, msg_btn_right, msg_btn_down, msg_btn_left, msg_btn_action, msg_btn_start };
		
		Promptlabel = new JLabel();
		Promptlabel.setBounds(20, 120, 400, 50);
		Promptlabel.setText(messages[occurence]);
		
		CloseButton = new HiddenButton(null, null, null);
		CloseButton.setBounds(376, 5, 15, 15);
		CloseButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// Execute when button is pressed
				jeu.requestFocus();
				dispose();
			}
		});		
		
		this.add(Promptlabel);
		this.add(CloseButton);	
		
		this.setFocusable(true);		
	}

	private class ConfigKeyListener implements KeyListener {
				 

		@Override
		public void keyPressed(KeyEvent e) {
			try {
				switch (occurence) {
				case 0:
					PropertyLoader.set(FILE_PROPERTIES, "up.key",
							String.valueOf(e.getKeyCode()));
					break;
				case 1:
					PropertyLoader.set(FILE_PROPERTIES, "right.key",
							String.valueOf(e.getKeyCode()));
					break;
				case 2:
					PropertyLoader.set(FILE_PROPERTIES, "down.key",
							String.valueOf(e.getKeyCode()));
					break;
				case 3:
					PropertyLoader.set(FILE_PROPERTIES, "left.key",
							String.valueOf(e.getKeyCode()));
					break;
				case 4:
					PropertyLoader.set(FILE_PROPERTIES, "action.key",
							String.valueOf(e.getKeyCode()));
					break;
				case 5:
					PropertyLoader.set(FILE_PROPERTIES, "start.key",
							String.valueOf(e.getKeyCode()));
					break;
				}
				occurence += 1;				
				
				if (occurence > 5)
				{
					jeu.requestFocus();
					dispose();
				}
				else
				{
					Promptlabel.setText(messages[occurence]);
				}
			} 
			catch (Exception ex) 
			{
				ex.printStackTrace();
			}
		}

		@Override
		public void keyReleased(KeyEvent arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void keyTyped(KeyEvent arg0) {
			// TODO Auto-generated method stub

		}
	}
}
